package com.atguigu.lease.admin.controller.lease;

import com.atguigu.lease.admin.service.LeaseAgreementService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.LeaseAgreement;
import com.atguigu.lease.model.vo.lease.AggrementItemVo;
import com.atguigu.lease.model.vo.lease.AggrementQueryVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name="租约管理模块")
@RestController
@RequestMapping("/admin/agreement")
public class AggrementController {
    @Autowired
    LeaseAgreementService leaseAgreementService;
    @Operation(summary = "1、带条件分页查询 租约列表")
    @GetMapping("/page")
    public Result<Page<AggrementItemVo>> pageItem(@RequestParam Long current,
                                                  @RequestParam Long size,
                                                  AggrementQueryVo aggrementQueryVo){
        /*
            查询租约分页列表时：
               方式1：
                    先查询分页的租约列表
                    然后再根据租约中的 公寓id 房间id 等信息 查询其他数据
                    最后组装数据为AggrementItemVo对象 到分页列表中

                    问题： 查询租约列表时，无法使用aggrementQueryVo中的 省市区id等条件过滤租约
                            租约表中没有省市区的id字段
               方式2：自定义sql：
                    直接多表联查，有条件直接可以对数据进行过滤
         */
        return Result.ok(leaseAgreementService.pageItem(current,size,aggrementQueryVo));
    }
    /// admin/agreement/saveOrUpdate
    @Operation(summary = "2、新增更新租约")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody LeaseAgreement leaseAgreement) {
        leaseAgreementService.saveOrUpdate(leaseAgreement);
        return Result.ok();
    }

    @Operation(summary = "3、根据id查询租约信息")
    @GetMapping
    public Result<AggrementItemVo> getAggrementItemVo(@RequestParam Long id) {
        return Result.ok(leaseAgreementService.getAggrementItemVo(id));
    }

    @Operation(summary = "4、根据id删除租约信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        return Result.ok(leaseAgreementService.removeById(id));
    }

    @Operation(summary = "5、根据id更新租约状态")
    @PostMapping("updateStatusById")
    public Result updateStatusById(@RequestParam Long id,
                                   @RequestParam String status) {
        leaseAgreementService.update(Wrappers.lambdaUpdate(LeaseAgreement.class)
                .eq(LeaseAgreement::getId, id)
                .set(LeaseAgreement::getStatus, status));
        return Result.ok();
    }
}